package com.zxt.content.user.manager;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxt.content.user.mapper.BUserMapper;
import com.zxt.content.user.model.BUser;
import com.zxt.content.user.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 通用逻辑层-用户管理类
 *
 * @author zxt
 */
@Slf4j
@Service
public class BUserManager extends ServiceImpl<BUserMapper, BUser> {

    /**
     * 根据用户名查找用户
     *
     * @param userName 用户名
     * @return 用户
     */
    public BUser getByUserName(String userName) {
        return getOne(BUser.gw().eq(BUser::getUserName, userName));
    }

    /**
     * 创建用户
     *
     * @param user 用户入参
     * @return id
     */
    public Long createUser(BUser user) {
        user.setId(null);
        user.setUserPwd(SecurityUtils.encryptPassword(user.getUserPwd()));
        super.save(user);
        return user.getId();
    }

    /**
     * 按照用户名修改登录时间和登录ip地址
     *
     * @param bUser
     */
    public void updateLoginByUserName(BUser bUser) {
        LambdaUpdateWrapper<BUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(BUser::getLoginDate, bUser.getLoginDate());
        wrapper.set(BUser::getLoginIp, bUser.getLoginIp());
        wrapper.eq(BUser::getUserName, bUser.getUserName());
        update(wrapper);
    }
}
